package com.ykdl.member.kid.adapters;

import java.util.List;

import com.ykdl.member.constant.KidAction;
import com.ykdl.member.kid.R;
import com.ykdl.member.kid.adapters.Coupon_New_FragmentAdapter.ViewHolder;
import com.ykdl.member.kid.adapters.Coupon_New_FragmentAdapter.onCheckListener;
import com.ykdl.member.kid.beans.OnLineCouponBean;
import com.ykdl.member.kid.beans.TicketBean;
import com.ykdl.member.kid.marketcard.CouponinfoWebView;
import com.ykdl.member.kid.util.DateUtil;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;
/********************************************************************************

** 作者： jiafeiyu
** 创始时间：2015-7-24
** 描述：
**    我的优惠券之在线可用优惠券列表
 * @param <E>

*********************************************************************************/
public class OnlineMyCouponAdapter extends BaseAdapter {
	private Context mContext;
	private LayoutInflater inflater;
	private int flag = -1;
	private List<OnLineCouponBean>  mOnLineList;
	public OnlineMyCouponAdapter(Context context,int number) {
		mContext = context;
		inflater =LayoutInflater.from(mContext);
		
		flag = number;
	}
	
	
	
	public void setmOnLineList(List<OnLineCouponBean> mOnLineList) {
		this.mOnLineList = mOnLineList;
	}



	@Override
	public int getCount() {
		if (mOnLineList!=null&&mOnLineList.size()>0) {
			return mOnLineList.size();
		}
		return 0;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView==null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.item_mycoupon_online, null);
			holder.tv_item_mycoupon_price = (TextView) convertView.findViewById(R.id.tv_item_mycoupon_price);
			holder.tv_item_coupon_online_type = (TextView) convertView.findViewById(R.id.tv_item_coupon_online_type);
			holder.tv_item_coupon_online_time = (TextView) convertView.findViewById(R.id.tv_item_coupon_online_time);
			holder.rl_my_coupon_online = (RelativeLayout) convertView.findViewById(R.id.rl_my_coupon_online);
			holder.tv_rmb = (TextView) convertView.findViewById(R.id.tv_rmb);
			convertView.setTag(holder);
		}else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		OnLineCouponBean info = mOnLineList.get(position);
		holder.rl_my_coupon_online.setTag(info);
		holder.rl_my_coupon_online.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO 页面详情的跳转
				OnLineCouponBean info = (OnLineCouponBean) v.getTag();
				Intent intent = new Intent(mContext, CouponinfoWebView.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				intent.putExtra(KidAction.COUPON_ID, info.getTicket_id()+"");
				intent.putExtra(KidAction.URL_TYPE, 7);
				mContext.startActivity(intent);
				
			}
		});

	
		if (!TextUtils.isEmpty(info.getCoupon_num_1())) {
			holder.tv_item_mycoupon_price.setText(info.getCoupon_num_1());
		}
		
		
		
		if (!TextUtils.isEmpty(info.getScope_desc())) {
			holder.tv_item_coupon_online_type.setText(info.getScope_desc());
		}
		
		
		switch (flag) {
		case 1:
			
			//未使用
			String start = DateUtil.getTimeDate(info.getValid_begin_time());
			String end = DateUtil.getTimeDate(info.getValid_end_time());
			String content = "使用期限  "+start+"-"+end;
			
			holder.tv_item_coupon_online_time.setText(content);
			break;
		case 2:
			holder.tv_item_coupon_online_time.setText("已使用");		
			break;
		case 3:
			int color = mContext.getResources().getColor(R.color.white);
			holder.rl_my_coupon_online.setBackgroundResource(R.drawable.item_coupon_online_outofdate);
			holder.tv_item_mycoupon_price.setTextColor(color);
			holder.tv_item_coupon_online_type.setTextColor(color);
			holder.tv_item_coupon_online_time.setTextColor(color);
			holder.tv_rmb.setTextColor(color);
			holder.tv_item_coupon_online_time.setText("已过期");	
			break;

		default:
			break;
		}
		return convertView;
	}

	/**
	 * 初始化 在线支付可用的优惠券
	 * @param position
	 * @param convertView
	 * @return
	 */
	public class ViewHolder{
		TextView tv_item_mycoupon_price;
		TextView tv_item_coupon_online_type;
		TextView tv_item_coupon_online_time;
		TextView tv_rmb;
		RelativeLayout rl_my_coupon_online;
	}
	/**
	 * 在线支付可用优惠券的领取事件
	 * @author jiafeiyu
	 *
	 */
	private onCheckListener mSelectListener;
	public interface onCheckListener{
		boolean onCheckListener(ViewHolder holder, TicketBean bean,int position);
	}

}
